package tlias.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tlias.exception.ClazzWithStudentException;
import tlias.mapper.ClazzMapper;
import tlias.mapper.StudentMapper;
import tlias.pojo.*;
import tlias.pojo.ClazzVO;
import tlias.service.ClazzService;

import java.util.List;

@Service
@Slf4j
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper studentMapper;

    public ClazzServiceImpl(ClazzMapper clazzMapper) {
        this.clazzMapper = clazzMapper;
    }

    @Override
    public PageResult<ClazzVO> list(ClazzQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        Page<ClazzVO> page = (Page<ClazzVO>) clazzMapper.list(param);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void delete(Integer[] ids) {
        List<Student> students = studentMapper.listByClazzId(ids);
        if (students.size() > 0) {
            throw new ClazzWithStudentException("该班级下有学生，无法删除");
        }
        clazzMapper.delete(ids);
    }

    @Override
    public void add(Clazz clazz) {
        clazzMapper.add(clazz);
    }

    @Override
    public void update(Clazz clazz) {
        clazzMapper.update(clazz);
    }

    @Override
    public ClazzVO getById(Integer id) {
        return clazzMapper.getById(id);
    }
    @Override
    public List<ClazzVO> listAll() {
        return clazzMapper.listAll();
    }
}